﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.Windows.Forms;


namespace Dreaming_of_Space_2
{
    public class SpaceShip
    {
       
       public int armor=100;
       public int max_armor = 100;
       public int cargo = 100;
       public int speed=2;
       public string ship_type;
       public int critical_damage = 10;
       public int fire_power = 1000;
       
       
       //создаем форватер для кораблей
       public SpaceTrace SpaceTrace1 = new SpaceTrace();

       public int pos_of_ufo_x, pos_of_ufo_y; 
       public int index=0, posX, posY;
       public double  dl, x1, x2, y1, y2;

       public SpaceShip() { pos_of_ufo_x = SpaceTrace1.way[0, 0]; pos_of_ufo_y = SpaceTrace1.way[0, 1];  }
        //оптимальный вариант случайная генерация трассы в виде ломаной кривой
       public bool InitTrace()
       {
           pos_of_ufo_x = SpaceTrace1.way[0, 0]; pos_of_ufo_y = SpaceTrace1.way[0, 1]; index = 0; dl = 0;

           return true;
       }
      
       public bool CalculateTrace()
       {
           if (index == SpaceTrace1.way.GetLength(0) - 1) return false;
           //движение корабля по траектории
           dl = dl + speed;

           posX = pos_of_ufo_x;
           posY = pos_of_ufo_y;

           //курс
           x1 = SpaceTrace1.way[index, 0];
           y1 = SpaceTrace1.way[index, 1];
           x2 = SpaceTrace1.way[index + 1, 0];
           y2 = SpaceTrace1.way[index + 1, 1];

           //расчет перемещения
           double tmp_x = 0, tmp_x1, tmp_x2, tmp_y;

           tmp_x1 = x1 + dl * (x2 - x1) / System.Math.Sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
           tmp_x2 = x1 - dl * (x2 - x1) / System.Math.Sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
           if ((x2 - x1 >= 0) && (tmp_x1 > x1)) tmp_x = tmp_x1;
           if ((x2 - x1 >= 0) && (tmp_x2 > x1)) tmp_x = tmp_x2;
           if ((x2 - x1 < 0) && (tmp_x1 < x1)) tmp_x = tmp_x1;
           if ((x2 - x1 < 0) && (tmp_x2 < x1)) tmp_x = tmp_x2;

          
           if (x2 - x1 == 0)
           {
               if (y2 - y1 < 0) { tmp_y = y1 - dl; tmp_x = x1; }
               else { tmp_y = y1 + dl; tmp_x = x1; }
           }
           else tmp_y = y1 + (tmp_x - x1) * (y2 - y1) / (x2 - x1);
 
           //корректировка курса
           if ((x2 - x1 <= 0) && (y2 - y1 <= 0) && (posX <= x2)/* && (posY <= y2)*/) { index++; dl = 0; }
           if ((x2 - x1 > 0) && (y2 - y1 < 0) && (posX >= x2) /*&& (posY <= y2)*/) { index++; dl = 0; }
           if ((x2 - x1 >= 0) && (y2 - y1 >= 0) && (posX >= x2)/* && (posY >= y2)*/) { index++; dl = 0; }
           if ((x2 - x1 < 0) && (y2 - y1 > 0) && (posX <= x2) /*&& (posY >= y2)*/) { index++; dl = 0; }


           pos_of_ufo_x = (int)System.Math.Ceiling(tmp_x);
           pos_of_ufo_y = (int)System.Math.Ceiling(tmp_y);
           return true;
       }

       public bool Redraw_Ufo(PaintEventArgs e)
       {
           //Создаем и рисуем летающую тарелку:
           if (armor <= 0) return false;
           Rectangle myRectangle1 = new Rectangle(
           new Point(pos_of_ufo_x - 10, pos_of_ufo_y - 10),
           new Size(20, 20));
           e.Graphics.DrawRectangle(new Pen(Color.Transparent),
                                                       myRectangle1);
           LinearGradientBrush myBrush1;

           if (armor > critical_damage)
               myBrush1 = new LinearGradientBrush(myRectangle1,
                   Color.LightGreen, Color.DarkGreen,
                   LinearGradientMode.Horizontal);
           else
               myBrush1 = new LinearGradientBrush(myRectangle1,
               Color.Orange, Color.DarkRed,
               LinearGradientMode.Horizontal);

           e.Graphics.FillEllipse(myBrush1, myRectangle1);

           Rectangle myRectangle2 = new Rectangle(
           new Point(pos_of_ufo_x - 5, pos_of_ufo_y - 5),
           new Size(10, 10));
           e.Graphics.DrawRectangle(new Pen(Color.Transparent),
                                                       myRectangle2);
           LinearGradientBrush myBrush2;
           if (armor > critical_damage)
               myBrush2 = new LinearGradientBrush(myRectangle2,
                   Color.LightGreen, Color.DarkGreen,
                   LinearGradientMode.Horizontal);
           else
               myBrush2 = new LinearGradientBrush(myRectangle2,
               Color.Orange, Color.DarkRed,
               LinearGradientMode.Horizontal);
           e.Graphics.FillEllipse(myBrush2, myRectangle2);
         
           int life = (int)System.Math.Ceiling((double)20*armor/max_armor); 
           Pen myPen = new Pen(Color.LightGreen, 2);
           e.Graphics.DrawLine(myPen, pos_of_ufo_x - 10, pos_of_ufo_y - 15, pos_of_ufo_x - 10+life, pos_of_ufo_y - 15);

           return true;
       }

        public bool UfoOpenFire(PaintEventArgs e, Planet TargetPlanet)
        {
            int target_x = TargetPlanet.pos_x, target_y = TargetPlanet.pos_y, target_R = TargetPlanet.R_orbit;

                if ((pos_of_ufo_x - target_x) * (pos_of_ufo_x - target_x) +
                     (pos_of_ufo_y - target_y) * (pos_of_ufo_y - target_y) < target_R*target_R)
                {
                    TargetPlanet.planet_life -= fire_power;
                    fire_power = 0;
                    Pen myPen = new Pen(Color.Red, 2);
                    e.Graphics.DrawLine(myPen, pos_of_ufo_x, pos_of_ufo_y,
                        target_x, target_y);

                   
                    return true;
                }
                return false;
              }

    }

}
    



